﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GBMS.APP;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.BLL;
using System.Configuration;
using System.Data;
using Telerik.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;
using KPIS.GERP.GBMS.MODEL;
using System.Collections;
using System.Threading;
using System.Security;

namespace KPIS.GERP.GBMS.APP_UI
{
    public partial class BGM_Approval : System.Web.UI.Page
    {
        private IProjectControl projectBLL;
        private IProjectHeader projectHeaderBLL;
        private ComboBLL comboBLL = new ComboBLL();
        private FlexibleFieldBLL FlexibleBLL = new FlexibleFieldBLL();
        private SystemTypeYearBLL sysTypeYearBLL = new SystemTypeYearBLL();
        int year = 2554;

        string type;

        public int BgmYear
        {
            set { year = value; }
            get { return year; }
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            type = Request.QueryString["type"].ToString();
            hdHeaderID.Value = Request.QueryString["id"].ToString();
            hdType.Value = type;

            if (type.Length > 0)
            {
                projectBLL = Project_Factory.GetProjectControlFactory(type);
                projectHeaderBLL = Project_Factory.GetProjectHeaderFactory(type);
            }

            if (!IsPostBack)
            {
                InitializedPage();

                LoadMasterData();
                LoadData();
            }
        }

        private void InitializedPage()
        {
            string type = Request.QueryString["type"].ToString();

            if (type == "md")
            {
                gridProject.Columns[4].HeaderText = "ผ่านการพิจารณา";
                gridProjectByOption.Columns[2].HeaderText = "ผ่านการพิจารณา";
                gridSource.Columns[5].HeaderText = "ผ่านการพิจารณา";
                gridMarket.Columns[3].HeaderText = "ผ่านการพิจารณา";
                gridProduct.Columns[3].HeaderText = "ผ่านการพิจารณา";
            }
            else if (type == "rc")
            {
                gridProject.Columns[4].HeaderText = "ได้รับจัดสรร";
                gridProjectByOption.Columns[2].HeaderText = "ได้รับจัดสรร";
                gridSource.Columns[5].HeaderText = "ได้รับจัดสรร";
                gridMarket.Columns[3].HeaderText = "ได้รับจัดสรร";
                gridProduct.Columns[3].HeaderText = "ได้รับจัดสรร";
            }
        }

        private void LoadData()
        {
            DataTable dt = projectHeaderBLL.GetDataProjectHeaderBySequence(hdHeaderID.Value);

            if (dt.Rows.Count > 0)
            {
                lbHeaderName.Text = (dt.Rows[0]["bgm_prj_header_name"].ToString().Length > 0) ? dt.Rows[0]["bgm_prj_header_name"].ToString() : "-";
                lbHeaderDescr.Text = (dt.Rows[0]["bgm_prj_header_descr"].ToString().Length > 0 ? dt.Rows[0]["bgm_prj_header_descr"].ToString() : "-");
            }

            ViewState["gridProject"] = projectBLL.GetDataStrategyControl(year, hdHeaderID.Value, false, "A");

            gridProject.DataSource = (DataTable)ViewState["gridProject"];
            gridProject.DataBind();

            ViewState["gridProjectByOption"] = projectBLL.GetDataProjectByOption(year, hdHeaderID.Value, "", "A");

            gridProjectByOption.DataSource = (DataTable)ViewState["gridProjectByOption"];
            gridProjectByOption.DataBind();

            ViewState["gridSource"] = projectBLL.GetDataProjectBySequenceForGridIn(hdHeaderID.Value, "");

            gridSource.DataSource = (DataTable)ViewState["gridSource"];
            gridSource.DataBind();

            ViewState["gridMarket"] = projectBLL.GetDataProjectOption(hdHeaderID.Value, "2", "");

            gridMarket.DataSource = (DataTable)ViewState["gridMarket"];
            gridMarket.DataBind();

            ViewState["gridProduct"] = projectBLL.GetDataProjectOption(hdHeaderID.Value, "1", "");

            gridProduct.DataSource = (DataTable)ViewState["gridProduct"];
            gridProduct.DataBind();

        }

        private void LoadMasterData()
        {
            //ddlStrategy.DataSource = comboBLL.ListStrategy(year.ToString());
            //ddlStrategy.DataBind();

            //ddlStrategy.DataValueField = "Sequence";
            //ddlStrategy.DataTextField = "bgm_prj_name";

            //ddlStrategy.Items.Insert(0, new RadComboBoxItem("----- กิจกรรมย่อยทั้งหมด -----", ""));

            //ddlProj.DataSource = comboBLL.ListOptionCombo("3");
            //ddlProj.DataBind();

            //ddlProj.DataValueField = "value";
            //ddlProj.DataTextField = "text";

            //ddlProj.Items.Insert(0, new RadComboBoxItem("----- โครงการบูรณาการทั้งหมด -----", ""));
        }

        protected void ddlStrategy_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            gridProject.DataSource = ((DataTable)ViewState["gridProject"]);
            gridProject.DataBind();

            gridProjectByOption.DataSource = ((DataTable)ViewState["gridProjectByOption"]);
            gridProjectByOption.DataBind();

            gridSource.DataSource = ((DataTable)ViewState["gridSource"]);
            gridSource.DataBind();

            gridMarket.DataSource = ((DataTable)ViewState["gridMarket"]);
            gridMarket.DataBind();

            gridProduct.DataSource = ((DataTable)ViewState["gridProduct"]);
            gridProduct.DataBind();
        }

        protected void gridSource_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                var dataItem = e.Item as GridDataItem;

                if (Convert.ToDouble(dataItem["approve_amount"].Text) < Convert.ToDouble(dataItem["total_amount"].Text))
                {
                    dataItem["total_amount"].Style["font-weight"] = "bold";
                    dataItem["approve_amount"].Style["font-weight"] = "bold";
                    dataItem["bgm_source_name"].Style["font-weight"] = "bold";
                    dataItem["approve_amount"].Style["color"] = "Red";
                    dataItem["bgm_source_name"].Style["color"] = "Red";
                }
                else if (Convert.ToDouble(dataItem["approve_amount"].Text) > Convert.ToDouble(dataItem["total_amount"].Text))
                {
                    dataItem["total_amount"].Style["font-weight"] = "bold";
                    dataItem["approve_amount"].Style["font-weight"] = "bold";
                    dataItem["bgm_source_name"].Style["font-weight"] = "bold";
                    dataItem["approve_amount"].Style["color"] = "Blue";
                    dataItem["bgm_source_name"].Style["color"] = "Blue";
                }
            }

            if (e.Item is GridFooterItem)
            {
                GridFooterItem footer = (GridFooterItem)e.Item;
                footer["bgm_source_name"].Controls.Add(new LiteralControl("<span style='color: Black; font-weight: bold;'>รวมทั้งหมด :</span> "));
            }
        }

        protected void gridProject_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                var dataItem = e.Item as GridDataItem;

                if (Convert.ToDouble(dataItem["approve_amount"].Text) < Convert.ToDouble(dataItem["total_amount"].Text))
                {
                    dataItem["total_amount"].Style["font-weight"] = "bold";
                    dataItem["approve_amount"].Style["font-weight"] = "bold";
                    dataItem["name"].Style["font-weight"] = "bold";
                    dataItem["approve_amount"].Style["color"] = "Red";
                    dataItem["name"].Style["color"] = "Red";
                }
                else if (Convert.ToDouble(dataItem["approve_amount"].Text) > Convert.ToDouble(dataItem["total_amount"].Text))
                {
                    dataItem["total_amount"].Style["font-weight"] = "bold";
                    dataItem["approve_amount"].Style["font-weight"] = "bold";
                    dataItem["name"].Style["font-weight"] = "bold";
                    dataItem["approve_amount"].Style["color"] = "Blue";
                    dataItem["name"].Style["color"] = "Blue";
                }
            }
            
            if (e.Item is GridFooterItem)
            {
                GridFooterItem footer = (GridFooterItem)e.Item;
                footer["name"].Controls.Add(new LiteralControl("<span style='color: Black; font-weight: bold;'>รวมทั้งหมด :</span> "));
            }
        }

        protected void gridProjectByOption_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                var dataItem = e.Item as GridDataItem;

                if (Convert.ToDouble(dataItem["approve_amount"].Text) < Convert.ToDouble(dataItem["total_amount"].Text))
                {
                    dataItem["total_amount"].Style["font-weight"] = "bold";
                    dataItem["approve_amount"].Style["font-weight"] = "bold";
                    dataItem["name"].Style["font-weight"] = "bold";
                    dataItem["approve_amount"].Style["color"] = "Red";
                    dataItem["name"].Style["color"] = "Red";
                }
                else if (Convert.ToDouble(dataItem["approve_amount"].Text) > Convert.ToDouble(dataItem["total_amount"].Text))
                {
                    dataItem["total_amount"].Style["font-weight"] = "bold";
                    dataItem["approve_amount"].Style["font-weight"] = "bold";
                    dataItem["name"].Style["font-weight"] = "bold";
                    dataItem["approve_amount"].Style["color"] = "Blue";
                    dataItem["name"].Style["color"] = "Blue";
                }
            }

            if (e.Item is GridFooterItem)
            {
                GridFooterItem footer = (GridFooterItem)e.Item;
                footer["name"].Controls.Add(new LiteralControl("<span style='color: Black; font-weight: bold;'>รวมทั้งหมด :</span> "));
            }
        }

        protected void gridMarket_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                var dataItem = e.Item as GridDataItem;

                if (Convert.ToDouble(dataItem["approve_amount"].Text) < Convert.ToDouble(dataItem["total_amount"].Text))
                {
                    dataItem["total_amount"].Style["font-weight"] = "bold";
                    dataItem["approve_amount"].Style["font-weight"] = "bold";
                    dataItem["bgm_option_name"].Style["font-weight"] = "bold";
                    dataItem["approve_amount"].Style["color"] = "Red";
                    dataItem["bgm_option_name"].Style["color"] = "Red";
                }
                else if (Convert.ToDouble(dataItem["approve_amount"].Text) > Convert.ToDouble(dataItem["total_amount"].Text))
                {
                    dataItem["total_amount"].Style["font-weight"] = "bold";
                    dataItem["approve_amount"].Style["font-weight"] = "bold";
                    dataItem["bgm_option_name"].Style["font-weight"] = "bold";
                    dataItem["approve_amount"].Style["color"] = "Blue";
                    dataItem["bgm_option_name"].Style["color"] = "Blue";
                }
            }

            if (e.Item is GridFooterItem)
            {
                GridFooterItem footer = (GridFooterItem)e.Item;
                footer["bgm_option_name"].Controls.Add(new LiteralControl("<span style='color: Black; font-weight: bold;'>รวมทั้งหมด :</span> "));
            }
        }

        protected void gridProduct_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                var dataItem = e.Item as GridDataItem;

                if (Convert.ToDouble(dataItem["approve_amount"].Text) < Convert.ToDouble(dataItem["total_amount"].Text))
                {
                    dataItem["total_amount"].Style["font-weight"] = "bold";
                    dataItem["approve_amount"].Style["font-weight"] = "bold";
                    dataItem["bgm_option_name"].Style["font-weight"] = "bold";
                    dataItem["approve_amount"].Style["color"] = "Red";
                    dataItem["bgm_option_name"].Style["color"] = "Red";
                }
                else if (Convert.ToDouble(dataItem["approve_amount"].Text) > Convert.ToDouble(dataItem["total_amount"].Text))
                {
                    dataItem["total_amount"].Style["font-weight"] = "bold";
                    dataItem["approve_amount"].Style["font-weight"] = "bold";
                    dataItem["bgm_option_name"].Style["font-weight"] = "bold";
                    dataItem["approve_amount"].Style["color"] = "Blue";
                    dataItem["bgm_option_name"].Style["color"] = "Blue";
                }
            }

            if (e.Item is GridFooterItem)
            {
                GridFooterItem footer = (GridFooterItem)e.Item;
                footer["bgm_option_name"].Controls.Add(new LiteralControl("<span style='color: Black; font-weight: bold;'>รวมทั้งหมด :</span> "));
            }
        }

        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            if (e.Argument == "refresh")
            {
                LoadData();
            }
        }

        protected void gridProject_SelectedIndexChanged(object sender, EventArgs e)
        {
            var dataItem = gridProject.SelectedItems[0] as GridDataItem;

            Session["SrtategyID"] = dataItem["bgm_strat_list_seq"].Text;
            Session["StrategyName"] = dataItem["name"].Text;
            lbStrategy.Text = dataItem["name"].Text;

            gridSource.DataSource = projectBLL.GetDataProjectBySequenceForGridIn(hdHeaderID.Value, dataItem["bgm_strat_list_seq"].Text);
            gridSource.DataBind();

            gridMarket.DataSource = projectBLL.GetDataProjectOption(hdHeaderID.Value, "2", dataItem["bgm_strat_list_seq"].Text);
            gridMarket.DataBind();

            gridProduct.DataSource = projectBLL.GetDataProjectOption(hdHeaderID.Value, "1", dataItem["bgm_strat_list_seq"].Text);
            gridProduct.DataBind();

            gridSource.Columns[0].Visible = true;
        }

        protected void gridProjectByOption_SelectedIndexChanged(object sender, EventArgs e)
        {
            var dataItem = gridProject.SelectedItems[0] as GridDataItem;
            
            Session["ControlID"] = dataItem["ControlID"].Text;
            Session["StrategyName"] = dataItem["name"].Text;

            gridSource.DataSource = projectBLL.GetDataProjectBySequenceForGridIn(hdHeaderID.Value, dataItem["bgm_strat_list_seq"].Text);
            gridSource.DataBind();

            gridMarket.DataSource = projectBLL.GetDataProjectOption(hdHeaderID.Value, "2", dataItem["bgm_strat_list_seq"].Text);
            gridMarket.DataBind();

            gridProduct.DataSource = projectBLL.GetDataProjectOption(hdHeaderID.Value, "1", dataItem["bgm_strat_list_seq"].Text);
            gridProduct.DataBind();

        }

        protected void ShowAll_Click(object sender, EventArgs e)
        {
            gridSource.Columns[0].Visible = false;

            LoadData();
        }
    }
}